<template>
  <div class="chat">
    <!-- 登录表单 -->
    <div class="login-form-container">
      <el-form
        ref="form"
        :model="loginForm"
        :rules="loginFormRules"
        label-width="80px"
        class="login-form"
      >
        <h1 style="text-align: center">ChatCove</h1>
        <el-form-item label="用户名" prop="userName">
          <el-input
            v-model="loginForm.userName"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="loginForm.password"
            placeholder="请输入密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item label="登录身份" prop="userType">
          <el-radio-group v-model="loginForm.userType" class="ml-4">
            <el-radio label="0" size="large"> 普通用户</el-radio>
            <el-radio label="1" size="large">管理员</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
  
<script setup>
import { reactive, ref } from "vue";
import api from '@/api/service'
import { useRouter } from "vue-router";
import {useUser} from '@/store/index'
const form = ref(null);
const router = useRouter();
const loginForm = reactive({
  userName: "",
  password: "",
  userType: "0",
});
const loginFormRules = ref({
  userName: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
});

const login = () => {
  form.value.validate(valid =>{
  if(valid) api.login(loginForm).then(resp =>{
    console.log(resp)
    if(resp.message.includes("登录成功")){
      window.localStorage.setItem('token', resp.token);
      var store  = useUser();
      const tempUser = JSON.parse(resp.data);
      store.userId = tempUser.userId;
      store.userName = tempUser.userName;
      store.avatar = tempUser.avatar; 
      store.userType = tempUser.userType
      // window.localStorage.setItem('data',resp.data);
      router.push('/');
    }

  });
  else return false;
  })
};
</script>
<style scoped>
@keyframes chat {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}

/*聊天 页面样式  */
.chat {
  display: flex;
  flex: 1;
  justify-content: center;
  align-items: center;
  background-image: linear-gradient(to right, #5fc3e4 0%, #e55d87 100%);
  height: 100vh;
  animation: chat 1s; /* 和上面对应 */
}
.login-form-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 60vh;
}

.login-form {
  width: 400px;
  padding: 20px;
  border: 1px solid #ebeef5;
  border-radius: 5px;
  background-color: #ffffff;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.login-form-item {
  margin-bottom: 20px;
}

.login-form-button {
  width: 100%;
}
</style>
  